<!-- 登录页面 组件 -->
<template>
    <div id="login">
        <div class="login-container">
            <h1>资源管理系统</h1>
            <div id="myform">
                <el-form ref="form" :model="form" :rules="rules">
                    <!-- 用户 表单控件 -->
                    <el-form-item label="用户名（以字母开头）" prop="userName">
                        <el-input v-model="form.userName" placeholder="请输入用户名" prefix-icon="el-icon-user-solid"
                            clearable></el-input>
                    </el-form-item>
                    <!-- 密码 表单控件 -->
                    <el-form-item label="密码（长度不小于3）" prop="userPwd">
                        <el-input show-password v-model="form.userPwd" placeholder="请输入密码"
                            prefix-icon="el-icon-s-goods"></el-input>
                    </el-form-item>
                    <!-- 按钮 表单控件 -->
                    <el-form-item>
                        <el-button class="elbtn" type="primary" @click="login" :loading="isLoading">登录</el-button>
                        <br>
                        <el-button class="elbtn" type="danger" @click="reset">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
// 引入API请求函数
import { loginAPI } from '@/api/user.js';
// 引入vuex
import { mapMutations } from 'vuex';
// 引入路由器中的loadAsyncRoute函数，登录后动态加载路由
import { loadAsyncRoute } from '@/router';

export default {
    name: 'Login',
    data() {
        return {
            isLoading: false, // 按钮加载中icon
            form: {
                userName: 'admin',
                userPwd: '123456',
            },
            rules: {
                userName: [
                    { required: true, message: '用户名不能为空', trigger: 'blur' },
                    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: ['change', 'blur'] },
                    { pattern: /^[a-zA-Z]/, message: '用户名只能以字母开头', trigger: ['change', 'blur'] },
                    // { type: 'email', message: '请输入邮箱地址', trigger: 'blur' },
                ],
                userPwd: [
                    { required: true, message: '密码不能为空', trigger: 'blur' },
                    { min: 3, message: '密码长度至少3位', trigger: ['change', 'blur'] },
                ],
            },
        };
    },
    computed: {

    },
    methods: {
        // 引入vuex Mutations
        ...mapMutations(['setUserInfo']),
        // 重置表单
        reset() {
            this.$refs.form.resetFields();
        },
        // 登录
        login() {
            // 表单校验
            this.$refs.form.validate(async (valid) => {
                if (!valid) {
                    // 表单校验失败
                    this.$message.error('表单校验失败');
                    return;
                };

                // 按钮加载中
                this.isLoading = true;
                // 发起请求
                let [data, err] = await loginAPI(this.form);
                // 取消按钮加载中
                this.isLoading = false;

                // 错误，跳出
                if (err) return;

                // console.log(data);
                // 将用户登录信息userInfo保存到vuex中
                this.setUserInfo(data.data);
                // this.$store.commit('setUserInfo', data.data);

                // 跳转页面
                let toPath = this.$route.query.toPath;
                this.$router.replace(toPath ? decodeURI(toPath) : '/');
                // 获取权限列表，动态加载路由
                loadAsyncRoute();
            });
        },
    }
};
</script>

<style lang="scss" scoped>
@keyframes moving {
    0% {
        background-position-x: left;
    }

    100% {
        background-position-x: right;
    }
}

#login {
    width: 100vw;
    height: 100vh;
    // background-color: #e2e2e2;
    background: linear-gradient(135deg, #f66, #f90, #3c9, #09f, #66f) left center/400% 400%;
    display: flex;
    justify-content: center;
    align-items: center;
    animation: moving 10s linear infinite alternate;

    .login-container {
        width: 400px;
        border-radius: 20px;
        background-color: #fff;
        padding: 20px 30px 10px;
        box-sizing: border-box;

        >h1 {
            text-align: center;
            margin-bottom: 20px;
        }

        .elbtn {
            width: 100%;
            margin-bottom: 10px;
        }
    }
}
</style>